package com.dhcc.wb.articleliushui;

import java.util.List;

import org.apache.log4j.Logger;

import com.dhcc.common.database.DBManager;
import com.dhcc.common.util.StringUtil;
import com.dhcc.wb.articleday.ArticleModel;

public class ArticleLiushuiDao {
	private static final Logger logger = Logger.getLogger(ArticleLiushuiDao.class);
	
	
	
	
	
	public Integer queryCount(String title) {
		DBManager dbm = null;
		Integer count = 0;
		try {
			dbm = new DBManager();
			String sql = "select count(*) from weixin_article_liushui g where 1=1 ";
		    if(!StringUtil.isNullOrEmpty(title)){
				   sql += " and title like  '%"+ title +"%'";			   
			   }
		    sql += "group by msgid";
			count = dbm.executeQueryCount(sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbm.close();
		}
		return count;
	}
	
	
	/*
	 * 文章流水查询
	 */
	@SuppressWarnings("unchecked")
	public List<ArticleDetailsModel> findArticleService2ByPage(Integer pageNumber,Integer pageSize,String title,String gzhname) {
		DBManager dbm = null;
		List<ArticleDetailsModel> list = null;
		try {
			dbm = new DBManager();
			String sql = "select * from weixin_article_liushui t where 1=1 ";
		    if(!StringUtil.isNullOrEmpty(title)){
			   sql += " and title like  '%"+ title +"%'";		   
		    }
		    if(!StringUtil.isNullOrEmpty(gzhname)){
				   sql += " and gzhname like  '%"+ gzhname +"%'";		   
			    }
		    sql += " order by stat_date desc";
	        sql +=  " limit " + pageNumber + ","+ pageSize +"";
			list = dbm.getObjectList(ArticleDetailsModel.class, sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbm.close();
		}
		return list;
	}
	
	public Integer queryCount2(String title) {
		DBManager dbm = null;
		Integer count = 0;
		try {
			dbm = new DBManager();
			String sql = "select count(*) from weixin_article_liushui g where 1=1 ";
		    if(!StringUtil.isNullOrEmpty(title)){
				   sql += " and title like  '%"+ title +"%'";			   
			   }
			count = dbm.executeQueryCount(sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbm.close();
		}
		return count;
	}
	
	
	
	//新增
	public int addArticleLiushui(String ids,String gzhid,String gzhname, String ref_date,String msgid,String title,String stat_date,Integer int_page_read_user,
			Integer int_page_read_count,Integer ori_page_read_user,Integer ori_page_read_count,
			Integer share_user,Integer share_count,Integer add_to_fav_user,Integer add_to_fav_count) {
		int i=0;
		DBManager dbm = null;
		try{
			dbm = new DBManager();
			String sql = "insert into weixin_article_liushui(ids,gzhid,gzhname,ref_date,msgid,title,stat_date,int_page_read_user,int_page_read_count,ori_page_read_user," +
					"ori_page_read_count,share_user,share_count,add_to_fav_user,add_to_fav_count) values('"+ ids +"','"+gzhid+"','"+gzhname+"','"+ ref_date +"','"+ msgid +"','"+title+"','"+stat_date+"','"+int_page_read_user+
					"','"+int_page_read_count+"','"+ori_page_read_user+"','"+ori_page_read_count+"','"+share_user+"','"+share_count+"','"+add_to_fav_user+"','"+add_to_fav_count+"')";
			i = dbm.executeUpdate(sql);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbm.close();
		}
		return i;
	}
	
	
	
	//查询唯一性
	public ArticleDetailsModel queryArticle_result(String title,String msgid,String stat_date,String gzhid) {
		ArticleDetailsModel model = null;
		DBManager dbm = null;
		try{
			dbm = new DBManager();
			String sql = "select * from weixin_article_liushui "
					+ " where title = '"+ title +"' and msgid='"+msgid+"' and stat_date = '"+stat_date+"' and gzhid ='"+gzhid+"'";
			model = (ArticleDetailsModel) dbm.getObject(ArticleDetailsModel.class, sql);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbm.close();
		}
		return model;
	}
	
	//查询文章的最大阅读人数
	public ArticleDetailsModel queryArticleMax_result(String msgid) {
		ArticleDetailsModel model = null;
		DBManager dbm = null;
		try{
			dbm = new DBManager();
			String sql = "select * from weixin_article_liushui "
					+ " where msgid = '"+ msgid +"' order by stat_date desc limit 0,1";
			model = (ArticleDetailsModel) dbm.getObject(ArticleDetailsModel.class, sql);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbm.close();
		}
		return model;
	}
		
	
	
	
		
}
